<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE html>
<html>
<head>
<title>楼栋单元管理</title>
<jsp:include page="../../views/layout/inc.jsp"></jsp:include>
<%-- <c:if test="${fn:contains(sessionInfo.resourceList, '/buildingUnitManager/editPage')}"> --%>
	<script type="text/javascript">
		$.canEdit = true;
	</script>
<%-- </c:if> --%>
<%-- <c:if test="${fn:contains(sessionInfo.resourceList, '/buildingUnitManager/delete')}"> --%>
	<script type="text/javascript">
		$.canDelete = true;
	</script>
<%-- </c:if> --%>
<%-- <c:if test="${fn:contains(sessionInfo.resourceList, '/buildingUnitManager/grantPage')}"> --%>
	<script type="text/javascript">
		$.canProperty = true;
	</script>
<%-- </c:if> --%>
<%-- <c:if test="${fn:contains(sessionInfo.resourceList, '/buildingUnitManager/editPwdPage')}"> --%>
	<script type="text/javascript">
		$.canResource= true;
	</script>
<%-- </c:if> --%>

<script type="text/javascript">
	//判断是否是系统管理员
	$.isAdmin = true;
</script>
<script type="text/javascript">
	var datagrid;
	$(function() {
		var residenceId = '${residence.id}';
		$('#residenceId').combobox({
			onSelect : function(a){
				$('#parentId').combobox({
					url : '/buildingManager/combobox?residenceId=' + a.id
				});
			},
			onLoadSuccess : function(e){
				$('#residenceId').combobox('clear');
			}
		});
		var url = '/buildingUnitManager/dataGrid';
		if(residenceId && residenceId != ''){
			url += "?residenceId=" + residenceId;
		}
		datagrid = $('#datagrid').datagrid({
			url : url,
			fit : true,
			fitColumns : true,
			border : false,
			pagination : true,
			idField : 'id',
			pageSize : 10,
			rowNumber:true,
			pageList : [ 10, 20, 30, 40, 50 ],
			sortName : 'id',
			sortOrder : 'asc',
			singleSelect : true,
			checkOnSelect : false,
			selectOnCheck : false,
			nowrap : true,
			frozenColumns :[[]] ,
			columns : [ [ {
				field : 'residenceName',
				title : '小区名字',
				width : 150,
			},{
				field : 'id',
				title :'id',
				width : 150,
				hidden : true
			},{
				field : 'residenceId',
				title :'residenceId',
				width : 150,
				hidden : true
			},{
				field : 'indexId',
				title :'index',
				width : 150,
				hidden : true
			},{
				field : 'parentName',
				title :'楼栋名称',
				width : 150,
				sortable : true
			},{
				field : 'name',
				title :'单元名称',
				width : 150,
				sortable : true
			},{
				field : 'sortWeight',
				title :'排序',
				width : 150,
				sortable : true
			},{
				field : 'gmtCreate',
				title : '创建时间',
				width : 150,
				sortable : true,
				formatter:function(val,rec){ 
					if(!val || val == '') return '';
					 var unixTimestamp = new Date(val);  
                     return unixTimestamp.format('yyyy-MM-dd hh:mm:ss');  
				}
			}, {
				field : 'gmtModify',
				title : '最后修改时间',
				width : 150,
				sortable : true,
				formatter:function(val,rec){ 
					if(!val || val == '') return '';
					var unixTimestamp = new Date(val);  
                    return unixTimestamp.format('yyyy-MM-dd hh:mm:ss');  
				}
			},{
				field : 'action',
				title : '操作',
				width : 100,
				formatter : function(value, row, index) {
					if(!row.id || row.id == '' || !row.residenceId ||row.residenceId== '-1') return '';
					var str = '';
					if ($.canEdit) {
						str += $.formatString('<img onclick="editFun(\'{0}\');" src="{1}" title="修改"/>', row.id, '/images/extjs_icons/pencil.png');
					}
					str += '&nbsp;';
					if ($.canDelete) {
						str += $.formatString('<img onclick="deleteFun(\'{0}\');" src="{1}" title="删除"/>', row.id, '/images/extjs_icons/cancel.png');
					}
					return str;
				}
			} ] ],
			toolbar : '#toolbar',
			onLoadSuccess : function() {
				$('#searchForm table').show();
				parent.$.messager.progress('close');

				$(this).datagrid('tooltip');
			},
			onRowContextMenu : function(e, rowIndex, rowData) {
				e.preventDefault();
				$(this).datagrid('unselectAll').datagrid('uncheckAll');
				$(this).datagrid('selectRow', rowIndex);
				$('#menu').menu('show', {
					left : e.pageX,
					top : e.pageY
				});
			}
		});
	});


	function deleteFun(id) {
		if (id == undefined) {//点击右键菜单才会触发这个
			var rows = datagrid.datagrid('getSelections');
			id = rows[0].id;
		} else {//点击操作里面的删除图标会触发这个
			datagrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		parent.$.messager.confirm('询问', '您是否要删除当前楼栋单元？', function(b) {
			if (b) {
				parent.$.messager.progress({
					title : '提示',
					text : '数据处理中，请稍后....'
				});
				$.post('/buildingUnitManager/delete', {
					id : id
				}, function(result) {
					if (result.success) {
						parent.$.messager.alert('提示', result.msg, 'info');
						datagrid.datagrid('reload');
					}
					parent.$.messager.progress('close');
				}, 'JSON');
			}
		});
	}

	function editFun(id) {
		if (id == undefined) {
			var rows = datagrid.datagrid('getSelections');
			id = rows[0].id;
		} else {
			datagrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		parent.$.modalDialog({
			title : '修改楼栋单元',
			width : 500,
			height : 300,
			href : '/buildingUnitManager/editPage?id=' + id,
			buttons : [ {
				text : '确认修改',
				handler : function() {
					parent.$.modalDialog.openner_datagrid = datagrid;//因为新增成功之后，需要刷新这个datagrid，所以先预定义好
					var f = parent.$.modalDialog.handler.find('#form');
					f.submit();
				}
			} ]
		});
	}

	function addFun() {
		var residenceId = '${residence.id}';
		if(!residenceId || residenceId == ''){
			try{
				 residenceId = $('#residenceId').combobox('getValue');
			} catch(e){}
		}
		var parentId = $('#parentId').combobox('getValue');
		parent.$.modalDialog({
			title : '新增楼栋单元',
			width : 500,
			height : 300,
			href : '/buildingUnitManager/addPage?residenceId='+residenceId+'&parentId='+parentId,
			buttons : [ {
				text :'确认新增',
				handler : function() {
					parent.$.modalDialog.openner_datagrid = datagrid;//因为新增成功之后，需要刷新这个datagrid，所以先预定义好
					var f = parent.$.modalDialog.handler.find('#form');
					f.submit();
				}
			} ]
		});
	}


	function searchFun() {
		datagrid.datagrid('load', $.serializeObject($('#searchForm')));
	}
	function cleanFun() {
		$('#searchForm input').val('');
		datagrid.datagrid('load', {});
	}
</script>
</head>
<body>
	<div class="easyui-layout" data-options="fit : true,border : false">
		<div data-options="region:'north',title:'查询条件',border:false" style="height: 110px; overflow: hidden;">
			<form id="searchForm">
				<table class="table table-hover table-condensed" style="display: none;">
				<c:if test="${isAdmin }">
						<tr>
							<th>小区名字</th>
							<td>
								<select  id="residenceId" name="residenceId"  type="text" placeholder="小区名字"   data-options="width:175,valueField:'id',textField:'text',icons:[{
												iconCls:'icon-clear',
												handler: function(e){
													$(e.data.target).combobox('setValue','');
												}
											}]" >
										<c:forEach items="${residences }" var="r">
												<option value="${r.id }">${r.name}</option>
										</c:forEach>
								</select>
							</td>
						</tr>
				</c:if>
						<tr>
							<th>楼栋名称</th>
							<td>
							<select  name="parentId" id="parentId"  type="text" placeholder="楼栋名称"  class="easyui-combobox"  data-options="width:175,icons:[{
												iconCls:'icon-clear',
												handler: function(e){
													$(e.data.target).combobox('setValue','');
												}
											}]" >
										<c:forEach items="${building }" var="b">
												<option value="${b.id }">${b.name}</option>
										</c:forEach>
								</select>
							</td>
						</tr>
					<tr>
						<th>单元名称</th>
						<td><input name="name" placeholder="楼栋名字" class="span2" /></td>
					</tr>
				</table>
			</form>
		</div>
		<div data-options="region:'center',border:false">
			<table id="datagrid"></table>
		</div>
	</div>
	<div id="toolbar" style="display: none;">
<%-- 		<c:if test="${fn:contains(sessionInfo.resourceList, '/buildingUnitManager/addPage')}"> --%>
			<a onclick="addFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'">新增</a>
<%-- 		</c:if> --%>
		<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="searchFun();">提交查询</a>
		<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-reload',plain:true" onclick="cleanFun();">重置查询</a>
	</div>

	<div id="menu" class="easyui-menu" style="width: 120px; display: none;">
<%-- 		<c:if test="${fn:contains(sessionInfo.resourceList, '/buildingUnitManager/addPage')}"> --%>
			<div onclick="addFun();" data-options="iconCls:'pencil_add'">增加</div>
<%-- 		</c:if> --%>
<%-- 		<c:if test="${fn:contains(sessionInfo.resourceList, '/buildingUnitManager/delete')}"> --%>
			<div onclick="deleteFun();" data-options="iconCls:'pencil_delete'">删除</div>
<%-- 		</c:if> --%>
<%-- 		<c:if test="${fn:contains(sessionInfo.resourceList, '/buildingUnitManager/editPage')}"> --%>
			<div onclick="editFun();" data-options="iconCls:'pencil'">修改</div>
<%-- 		</c:if> --%>
	</div>
</body>
</html>